using System;
using System.Collections.Generic;
using System.Text;
using Entity;
using eShop.Model;
using eShop.Business;

namespace eShop.Business
{
    public class Favorites
    {
        private int _favoriteId;
        private int _userId;
        private bool _isActive;
        private bool _isDeleted;
        private DateTime _createDate;
        private Product _product;

        public int FavoriteId
        {
            get { return _favoriteId; }
            set { _favoriteId = value; }

        }
        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        public Product Product
        {
            get { return _product; }
            set { _product = value; }
        }

        public DateTime CreateDate
        {
            get { return _createDate; }
            set { _createDate = value; }
        }

        
        public Favorites(TblFavorites tblFavorite)
        {
            this._favoriteId = tblFavorite.FavoritesId;
            this._userId = tblFavorite.UserId;
            this._product = ProductManager.GetProductById(tblFavorite.ProductId);
            this._isActive = tblFavorite.IsActive;
            this._isDeleted = tblFavorite.IsDeleted;
            this._createDate = tblFavorite.CreateDate;
        }
        public void Create()
        {
            TblFavorites tblFavorite = new TblFavorites();
            tblFavorite.UserId = _userId;
            tblFavorite.ProductId = Product.ProductId;
            tblFavorite.IsActive = true;
            tblFavorite.IsDeleted = false;
            tblFavorite.CreateDate = DateTime.Now;
            tblFavorite.Save();
            this._favoriteId = tblFavorite.FavoritesId;
        }

        public void Delete()
        {
            TblFavorites tblFavorite = TblFavorites.GetTblFavoritesByFavoritesId(this._favoriteId);
            tblFavorite.IsDeleted = true;
            tblFavorite.Save();
        }

        public static Favorites Load(int favoriteId)
        {
            TblFavorites tblFavorite = TblFavorites.GetTblFavoritesByFavoritesId(favoriteId);
            if (tblFavorite != null)
            {
                return new Favorites(tblFavorite);
            }
            else
            {
                return null;
            }
        }


        public static List<Favorites> GetFavoritesByUserId(int userId)
        {
            List<Favorites> favorites = new List<Favorites>();
            List<TblFavorites> tmpFavorites = TblFavoritesExtended.GetActiveFavoritesByUserId(userId);
            for (int i = 0; i < tmpFavorites.Count; i++)
            {
                favorites.Add(new Favorites(tmpFavorites[i]));
            }
            return favorites;
        }

        public static void SaveFavorites(List<Favorites> favorites, int userId, bool OverWriteExisting)
        {
            if (favorites.Count > 0)
            {
                List<Favorites> dbfavorites = new List<Favorites>(); ;
                if (OverWriteExisting)
                {
                    dbfavorites = Favorites.GetFavoritesByUserId(userId);
                }
                foreach (Favorites favorite in favorites)
                {
                    bool found = false;
                    if (OverWriteExisting)
                    {
                        foreach (Favorites tmpFavorite in dbfavorites)
                        {
                            if (tmpFavorite.Product.ProductId == favorite.Product.ProductId)
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found)
                    {
                        favorite.UserId = userId;
                        favorite.Create();
                    }
                }
            }
        }

        public static void DeleteFavorites(List<Favorites> favorites)
        {
            for (int i = 0; i < favorites.Count; i++)
            {
                favorites[i].Delete();
            }
        }
    }
}
